<!DOCTYPE html>


<html lang="zh-CN">


<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    Redis 安装和基础数据结构 |  
  </title>
  <meta name="generator" content="hexo-theme-ayer">
  
  <link rel="shortcut icon" href="/assets/img/favicon.ico" />
  
  <link rel="stylesheet" href="/dist/main.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">
  <link rel="stylesheet" href="/css/custom.css">
  
  

  
<script>
var _hmt = _hmt || [];
(function() {
	var hm = document.createElement("script");
	hm.src = "https://hm.baidu.com/hm.js?e6614c2d092d0bce54a2543d8cf949fa";
	var s = document.getElementsByTagName("script")[0]; 
	s.parentNode.insertBefore(hm, s);
})();
</script>


<link rel="alternate" href="/atom.xml" title="null" type="application/atom+xml">
</head>

</html>

<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-Redis-安装和基础数据结构"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  Redis 安装和基础数据结构
</h1>
 

    </header>
     
    <div class="article-meta">
      <a href="/2019/08/16/Redis-安装和基础数据结构/" class="article-date">
  <time datetime="2019-08-16T03:12:00.000Z" itemprop="datePublished">2019-08-16</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/技术/">技术</a>
  </div>
  
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">1.6k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">6 分钟</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <p>Redis 包含 5 中基础数据结构，分别为字符串 string 、列表 list 、字典 hash 、集合 set 和有序集合 zset 。</p>
<p>本篇文章简要介绍 Redis 的安装和这 5 种基础数据结构的使用。</p>
<a id="more"></a>
<h3 id="Redis-安装"><a href="#Redis-安装" class="headerlink" title="Redis 安装"></a>Redis 安装</h3><p>官网下载源码压缩包，如 <code>redis-5.0.5.tar.gz</code> ，解压后编译即可完成安装。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span> cd redis-5.0.5</span><br><span class="line"><span class="meta">$</span> make</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 运行 Redis 服务器</span><br><span class="line"><span class="meta">#</span> --daemonize yes 或在 redis.conf 中修改 daemonize 为 yes 表示后台运行</span><br><span class="line"><span class="meta">$</span> cd src</span><br><span class="line"><span class="meta">$</span> ./redis-server</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 运行命令行客户端</span><br><span class="line"><span class="meta">$</span> ./redis-cli</span><br></pre></td></tr></table></figure>
<h3 id="基础数据结构"><a href="#基础数据结构" class="headerlink" title="基础数据结构"></a>基础数据结构</h3><p>Redis 中所有的数据结构都以唯一的 key 作为名称，通过 key 来获取相应的 value。不同数据结构类型的差异在于 value 的结构不一样。</p>
<p>list 、 set 、hash 和 zset 这四种数据结构为容器型结构，对其进行操作时，如果 key 不存在则先创建再操作，最后一个元素被删除时数据结构被删除。</p>
<p>Redis 中可以对 key 设置过期时间，到时间会自动删除 key ，常用于控制缓存的失效时间。</p>
<p>需要注意的是，只能对整个 key 设置过期时间，不能对某个子 key 设置过期时间，另外对已经设置了过期时间的 key 进行修改，过期时间会消失。</p>
<h4 id="string"><a href="#string" class="headerlink" title="string"></a>string</h4><p>字符串是 Redis 中最简单的数据结构，可以动态地修改字符串，常见的用法如缓存用户信息。</p>
<p>string 的内部表示是一个字符数组，类似于 Java 中的 ArrayList ，通过预分配冗余空间来减少内存的频繁分配。</p>
<p>当字符串长度小于 1 MB 时，按现有空间加倍扩容，超过 1 MB 时每次扩容 1 MB ，最大长度为 512 MB 。</p>
<p>string 的操作命令如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span> 设置 name 的值为 Jack</span><br><span class="line"><span class="meta">&gt;</span> set name Jack</span><br><span class="line">OK</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 获取 name 的值</span><br><span class="line"><span class="meta">&gt;</span> get name</span><br><span class="line">"Jack"</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 判断是否存在键 name</span><br><span class="line"><span class="meta">&gt;</span> exists name</span><br><span class="line">(integer) 1</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 删除键 name</span><br><span class="line"><span class="meta">&gt;</span> del name</span><br><span class="line">(integer) 1</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 批量设置</span><br><span class="line"><span class="meta">&gt;</span> mset name1 Jack name2 Tony name3 Jay</span><br><span class="line">OK</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 批量获取</span><br><span class="line"><span class="meta">&gt;</span> mget name1 name2 name3</span><br><span class="line">1) "Jack"</span><br><span class="line">2) "Tony"</span><br><span class="line">3) "Jay"</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 设置 5 秒后过期</span><br><span class="line"><span class="meta">&gt;</span> expire name1 5</span><br><span class="line">(integer) 1</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 设置 key 且 5 秒后过期，相当于 set + expire</span><br><span class="line"><span class="meta">&gt;</span> setex name 5 Jack</span><br><span class="line">OK</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 如果 key 不存在就创建</span><br><span class="line"><span class="meta">&gt;</span> setnx name Jack</span><br><span class="line">OK</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 如果 value 为整数，可以对其进行自增操作</span><br><span class="line"><span class="meta">&gt;</span> set age 18</span><br><span class="line">OK</span><br><span class="line"><span class="meta">&gt;</span> incr age</span><br><span class="line">(integer) 19</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> age 的值加 5（可传入负数表示减操作）</span><br><span class="line"><span class="meta">&gt;</span> incrby age 5</span><br><span class="line">(integer) 24</span><br></pre></td></tr></table></figure>
<h4 id="list"><a href="#list" class="headerlink" title="list"></a>list</h4><p>Redis 中的列表相当于 Java 中的 LinkedList，是链表而非数组。插入和删除操作非常快，时间复杂度 O(1) ，但索引定位很慢，时间复杂度 O(n) 。</p>
<p>Redis 底层存储的不是简单的 LinkedList ，而是称为 QuickList 的结构。元素较少时使用连续的内存存储（ziplist 压缩列表），元素多时将多个 ziplist 组成链表，既满足快速插入删除的性能，又节约了空间。</p>
<p>Redis 列表常用做异步队列，将任务塞进列表，另一个线程从列表中轮询数据进行处理。</p>
<p>list 的操作命令如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span> 向右插入一个或多个元素</span><br><span class="line"><span class="meta">&gt;</span> rpush books b1 b2 b3</span><br><span class="line">(integer) 3</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 向左插入一个或多个元素</span><br><span class="line"><span class="meta">&gt;</span> lpush books b4 b5 b6</span><br><span class="line">(integer) 6</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 获取列表长度</span><br><span class="line"><span class="meta">&gt;</span> llen books</span><br><span class="line">(integer) 6</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 从左侧弹出一个元素</span><br><span class="line"><span class="meta">&gt;</span> lpop books</span><br><span class="line">"b6"</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 从右侧弹出一个元素</span><br><span class="line"><span class="meta">&gt;</span> rpop books</span><br><span class="line">"b3"</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 获取指定索引的元素</span><br><span class="line"><span class="meta">&gt;</span> lindex books 3</span><br><span class="line">"b2"</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 截取指定索引内的列表，仅保留截取范围内的区间</span><br><span class="line"><span class="meta">#</span> index 可以为负数，-1 表示倒数第一个元素，-2 表示倒数第二个元素</span><br><span class="line"><span class="meta">&gt;</span> ltrim books 1 2</span><br><span class="line">OK</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 获取指定索引区间的元素</span><br><span class="line"><span class="meta">&gt;</span> lrange books 0 -1</span><br><span class="line">1) "b4"</span><br><span class="line">2) "b1"</span><br></pre></td></tr></table></figure>
<h4 id="hash"><a href="#hash" class="headerlink" title="hash"></a>hash</h4><p>Redis 中的字典相当于 Java 中的 HashMap ，实现结构也与 Java 一致，即数组和链表的二维结构，一维 hash 数组位置碰撞时，将碰撞的元素用链表串起来。</p>
<p>Redis 中字典的值只能是字符串，另外 rehash 时采用了渐进式 rehash 策略。</p>
<p>渐进式 rehash 策略在执行时，会同时保留两个 hash 结构，查询时同时查询两个结构，由定时任务循序渐进地将旧 hash 中的数据转移到新 hash 中，全部转移完成用新的 hash 取代旧的，旧的 hash 被删除。</p>
<p>字典可用于按字段存储用户信息，从而不必获取全部的信息，不过 hash 结构的存储消耗要高于字符串。</p>
<p>hash 的操作命令如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span> 对指定键插入 key - value ，更新操作返回 0</span><br><span class="line"><span class="meta">&gt;</span> hset books redis RedisBook</span><br><span class="line">(integer) 1</span><br><span class="line"><span class="meta">&gt;</span> hset books java JavaBook</span><br><span class="line">(integer) 1</span><br><span class="line"><span class="meta">&gt;</span> hset books java NewJavaBook</span><br><span class="line">(integer) 0</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 获取指定字典的全部内容</span><br><span class="line"><span class="meta">&gt;</span> hgetall books</span><br><span class="line">1) "redis"</span><br><span class="line">2) "RedisBook"</span><br><span class="line">3) "java"</span><br><span class="line">4) "NewJavaBook"</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 获取字典长度</span><br><span class="line"><span class="meta">&gt;</span> hlen books</span><br><span class="line">(integer) 2</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 获取字典中指定 key 的 value</span><br><span class="line"><span class="meta">&gt;</span> hget books redis</span><br><span class="line">"RedisBook"</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 批量插入 key - value</span><br><span class="line"><span class="meta">&gt;</span> hmset books python PythonBook golang GolangBook</span><br><span class="line">OK</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 对字典中单个 key 进行计数操作</span><br><span class="line"><span class="meta">&gt;</span> hset books number 1</span><br><span class="line">(integer) 1</span><br><span class="line"><span class="meta">&gt;</span> hincrby books number 5</span><br><span class="line">(integer) 6</span><br></pre></td></tr></table></figure>
<h4 id="set"><a href="#set" class="headerlink" title="set"></a>set</h4><p>Redis 中的集合相当于 Java 中的 HashSet ，键值对无序且唯一，内部实现相当于一个特殊的字典，value 都是 NULL 。</p>
<p>集合可用于存储活动中奖用户 ID ，保证一个用户不会中奖两次。</p>
<p>set 的操作命令如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span> 向集合中插入一个或多个元素</span><br><span class="line"><span class="meta">&gt;</span> sadd books python</span><br><span class="line">(integer) 1</span><br><span class="line"><span class="meta">&gt;</span> sadd books python</span><br><span class="line">(integer) 0</span><br><span class="line"><span class="meta">&gt;</span> sadd books java golang</span><br><span class="line">(integer) 2</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 获取集合中全部元素</span><br><span class="line"><span class="meta">&gt;</span> smembers books</span><br><span class="line">1) "python"</span><br><span class="line">2) "golang"</span><br><span class="line">3) "java"</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 获取集合大小</span><br><span class="line"><span class="meta">&gt;</span> scard books</span><br><span class="line">(integer) 3</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 判断元素是否在集合中</span><br><span class="line"><span class="meta">&gt;</span> sismember books java</span><br><span class="line">(integer) 1</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 弹出一个元素</span><br><span class="line"><span class="meta">&gt;</span> spop books</span><br><span class="line">"golang"</span><br></pre></td></tr></table></figure>
<h4 id="zset"><a href="#zset" class="headerlink" title="zset"></a>zset</h4><p>Redis 中的有序集合 zset 既可以保证内部元素的唯一性，又可以给每一个元素赋予一个排序权重 score 。</p>
<p>有序集合可以用于存储学生成绩，元素即学生 ID ，score 为其考试成绩，可以按成绩排序获取名次。</p>
<p>zset 的操作命令如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span> 向有序集合中插入元素</span><br><span class="line"><span class="meta">&gt;</span> zadd books 1 book1</span><br><span class="line">(integer) 1</span><br><span class="line"><span class="meta">&gt;</span> zadd books 3 book2</span><br><span class="line">(integer) 1</span><br><span class="line"><span class="meta">&gt;</span> zadd books 2 book3</span><br><span class="line">(integer) 1</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 按 score 排序列出指定名次范围的元素</span><br><span class="line"><span class="meta">&gt;</span> zrange books 0 1</span><br><span class="line">1) "book1"</span><br><span class="line">2) "book3"</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 按 score 逆序列出指定名次范围的元素</span><br><span class="line"><span class="meta">&gt;</span> zrevrange books 0 1</span><br><span class="line">1) "book2"</span><br><span class="line">2) "book3"</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 获取集合大小</span><br><span class="line"><span class="meta">&gt;</span> zcard books</span><br><span class="line">(integer) 3</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 获取指定 value 的 score</span><br><span class="line"><span class="meta">&gt;</span> zscore books book1</span><br><span class="line">"1"</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 获取指定 value 的排名</span><br><span class="line"><span class="meta">&gt;</span> zrank books book1</span><br><span class="line">(integer) 0</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 按 score 区间遍历集合</span><br><span class="line"><span class="meta">&gt;</span> zrangebyscore books 0 2</span><br><span class="line">1) "book1"</span><br><span class="line">2) "book3"</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 删除指定 value</span><br><span class="line"><span class="meta">&gt;</span> zrem books book3</span><br><span class="line">(integer) 1</span><br></pre></td></tr></table></figure> 
      <!-- reward -->
      
      <div id="reword-out">
        <div id="reward-btn">
          打赏
        </div>
      </div>
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>版权声明： </strong>
          
          本博客所有文章除特别声明外，著作权归作者所有。转载请注明出处！
          
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=https://blog.ulyssesss.com/2019/08/16/Redis-安装和基础数据结构/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Redis/">Redis</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2019/08/19/Redis-分布式锁/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            Redis 分布式锁
          
        </div>
      </a>
    
    
      <a href="/2019/08/13/设计模式之访问者模式/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">设计模式之访问者模式</div>
      </a>
    
  </nav>

   
 
   
     
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2016-2021
        <i class="ri-heart-fill heart_icon"></i> 大雪碧
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/assets/img/head.svg" alt=""></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>谢谢老板</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/assets/img/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/assets/img/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    <script src="/js/jquery-2.0.3.min.js"></script>
<script src="/js/lazyload.min.js"></script>
<!-- Tocbot -->

<script src="/js/tocbot.min.js"></script>
<script>
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto'
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>
<script src="/dist/main.js"></script>
<!-- ImageViewer -->

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>

<!-- MathJax -->

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
      tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
  });

  MathJax.Hub.Queue(function() {
      var all = MathJax.Hub.getAllJax(), i;
      for(i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
      }
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/mathjax@2.7.6/unpacked/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script>
  var ayerConfig = {
    mathjax: true
  }
</script>

<!-- Katex -->

<!-- busuanzi  -->

<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->

<link rel="stylesheet" href="/css/clipboard.css">
<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>


<!-- CanvasBackground -->


    
  </div>
</body>

</html>